-- 7. Write your own definition of the standard takeWhile function, first using explicit
-- recursion, and then foldr.

takeWhile_recursion :: (a -> Bool) -> [a] -> [a]
takeWhile_recursion p (x:xs) = if p x
                               then x : takeWhile_recursion p xs
                               else []
takeWhile_recursion p [] = []

-- 测试数据
-- takeWhile_recursion (< 3) [1,2,3,4,1,2,3,4] == [1,2]
-- takeWhile_recursion (< 9) [1,2,3] == [1,2,3]
-- takeWhile_recursion (< 0) [1,2,3] == []


-- takeWhile_foldl p xs = foldl step [] xs
--   where step ys x = if p x
--                     then x : ys
--                     else ys
-- -- 测试数据
-- -- takeWhile_foldl (< 3) [1,2,3,4,1,2,3,4] == [1,2]

takeWhile_foldr p xs = foldr step [] xs
  where step x ys = if p x
                    then x : ys
                    else []
-- 测试数据
-- takeWhile_foldr (< 6) [1,2,7,8,3,4,7,8] == [1,2]


